package TomcatRelated;

import java.util.Vector;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLHandler extends DefaultHandler {

	Boolean currentElement = false;
	Vector<UserProfile> _allUsers = new Vector<UserProfile>();
	UserProfile _user;
	String _tmpValue;

	/** Called when tag starts ( ex:- <name>AndroidPeople</name>
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		currentElement = true;

		if (localName.equals("userProfile"))
		{
			_user = new UserProfile();
		}
	}

	/** Called when tag closing ( ex:- <name>AndroidPeople</name>
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
	throws SAXException {
		currentElement = false;
		System.out.println("localName="+localName+", qName="+qName);
		if (localName.equals("_android_id")) {
			_user.set_android_id(_tmpValue);
		}
		else if (localName.equals("_google_account")) {
			_user.set_google_account(_tmpValue);
		} 
		else if (localName.equals("_c2dm_register_id")) {
			_user.set_c2dm_register_id(_tmpValue);
		} 
		else if (localName.equals("x")) {
			_user.set_x(Double.parseDouble(_tmpValue));
			System.out.println("X_tmpValue = "+_tmpValue);
			System.out.println("_user.getX() = "+_user.getX());
		} 
		else if (localName.equals("y")) {
			_user.set_y(Double.parseDouble(_tmpValue));
			System.out.println("Y_tmpValue = "+_tmpValue);
			System.out.println("_user.getY() = "+_user.getY());
		}
		else if (localName.equals("userProfile")) {
			this._allUsers.add(_user);
		}
		else{ //error

		}
	}

	/** Called to get tag characters ( ex:- <name>AndroidPeople</name>
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
	throws SAXException {

		if (currentElement) {
			_tmpValue = new String(ch, start, length);
			currentElement = false;
		}

	}

	public UserProfile getUser() {
		return _user;
	}

	public Vector<UserProfile> getAllUsersProfiles() {
		return _allUsers;
	}
}
